// 推荐给用户的书籍

import 'package:flutter/material.dart';
import 'package:sharing/model/book.dart';
import 'package:sharing/ui/component/book_with_name.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sharing/utils/whole_apis.dart';
import 'package:sharing/net/ris_read_apis.dart';
import 'package:sharing/net/net_util.dart';
import 'package:dio/dio.dart';
import 'package:sharing/model/book_list.dart';
import 'package:sharing/model/book.dart';


class RecommendBooks extends StatefulWidget {
  List<Book> books;
  RecommendBooks({@required this.books}):assert(books!=null);

  @override
  _RecommendBooksState createState() => _RecommendBooksState();
}

class _RecommendBooksState extends State<RecommendBooks> {

  List<Book> _books = [];
  void _loadRecommends() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String authorization = await prefs.getString("authorization");
    NetUtil.originGet(WholeApis.hostName + RisReadApis.recommend,
        Options(headers: {"authorization": authorization}),
            (Response response) {
          print("data ${response.data}");
          var code = response.data["code"];
          var message = response.data["message"];
          if (code == 200 && message != null) {
            _books.clear();
            _books = Book.fromJson(message);
            setState(() {});
          }
        });
  }
  Widget _build(BuildContext context, int index) {
    return BookWithName(
      book: _books[index],
    );
  }
  @override
  Widget build(BuildContext context) {
    return Material(

      child:Container(
        height: 144.0,
        child:  ListView.builder(itemBuilder: _build,itemCount:_books==null?0: _books.length,scrollDirection: Axis.horizontal,),
      ),
    );
  }

  @override
  void initState() {
    _loadRecommends();
  }
}
